package cn.hls.web.controller.biz;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.SmsInfo;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.SmsInfoService;
import cn.hls.web.controller.base.BaseController;


/**
  *@fileName: SmsInfoController.java
  *@package: cn.hls.web.controller.biz
　*@Description:短信发送控制器
　*@author: yinghui.zhang
　*@email: uuhui@163.com
  *@date: 2016年12月25日
 *
 */
@Controller
@RequestMapping("/smsInfo")
public class SmsInfoController extends BaseController<SmsInfo> {
	@Resource(name = "smsInfoService")
	private SmsInfoService service;
	
	 
	/**
	 * 表单
	 * 
	 * @param id 编号
	 * @param show 是否查看操作 true:查看操作 false:新增或修改操作
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	public ModelAndView info(Long id,Integer type, boolean show )
			throws Exception {
		SmsInfo entity = null;
		ModelAndView modelAndView=new ModelAndView();
		if (id != null) {
			entity = service.find(id);
		}
		modelAndView.addObject("entity",entity);
		if (show) {
			modelAndView.setViewName("sms/show");
		} else {
			modelAndView.setViewName("sms/info");
		}
		return modelAndView;
	}

	/**
	 * 添加操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(SmsInfo entity,HttpServletRequest request) throws Exception {
		String memberIds=request.getParameter("memberIds");
		if(StringUtils.hasText(memberIds)){
			 String []tempArray=memberIds.split("\\,");
			 List<Long>list=new ArrayList<Long>();
			 for(String item:tempArray){
				 list.add(Long.parseLong(item));
			 }
			 entity.setMemberIds(list);
			 entity.setOperator(getCurrentUser(request).getLoginName());
			boolean flag = service.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		}else{
			msg.setInfo("发送短信人员信息不存在");
			msg.setStatus(201);
		}
		return msg;
	}

	 
	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		service.delete(tempIds);
		return msg;
	}
	 

	/**
	 * 分页查询
	 * @param entity 查询的会友对象
	 * @param page 分页对象
	 * @return PageInfo<Company>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<SmsInfo> list(SmsInfo entity, PageInfo<SmsInfo> page)
			throws Exception {
		return service.loadSmsInfo(page, entity);
	}
	 
	@RequestMapping("/view")
	public ModelAndView view(HttpServletRequest request) {
		return new ModelAndView("/sms/list");
	}
}
